/**
 * Copyright 2015 Qiyi Inc. All rights reserved.
 *
 * @file:   Carousel.Grid.js
 * @author: karkarrotxu@qiyi.com
 * @date:   2016-05-30
 */
qui(
    function (require, exports, module) {
        Carousel.AutomationRuleGrid = function (options) {
            this.dao = Carousel.Dao;
            this.config = Carousel.Config;
            this.task = Carousel.Task;
            this.options = options || {};
        };
        Com.inherits(Carousel.AutomationRuleGrid, Ancestor.Grid).extend({
            runAutomationRule: function () {
                var self = this;
                self.$grid = self.options.$grid;
                var taskArr = [];
                taskArr.push(self.task.getLiveProgramTypeTask(self.dao.cfgName.liveProgramType));
                taskArr.push(self.task.platformsTask(self.dao.cfgName.PlayPlatform))
                taskArr.push(self.task.getPlatformReferTask(self.dao.cfgName.PlatformRefer));
                var fatherId = Com.url.getParameter("fatherId");
                if (EpgWeb.Plugin.Validate.checkNum(fatherId)) {
                    self.dao.channelId = Number(fatherId);
                    taskArr.push(self.task.getChannel(self.dao.cfgName.getChannel, {id: self.dao.channelId}));
                }
                $.each(taskArr, function (index, element) {
                    EpgWeb.ajaxQueue.push(element.regName, element.options);
                })
                EpgWeb.ajaxQueue.afterLoadComplete(function(){
                    self.startAutomationRule();
                })
                EpgWeb.ajaxQueue.startLoad();
            },
            initSuggest: function () {
            },
            initDialogs: function () {
            },
            bindEvent: function () {
                $("button[name=btn_rule_start]").bind("click", function (event) {
                    Carousel.Util.changeStatusAutomationRule(1)
                })
                $("button[name=btn_rule_stop]").bind("click", function (event) {
                    SystemDialog.queryConfirm(Carousel.Config.Hint.STOP_RULE_HINT, function (flag) {
                        if (flag === true) {
                              Carousel.Util.changeStatusAutomationRule(0)
                        }
                    })
                })
                $("#carouselAutoRuleView").find("button[name=addRule]").bind("click", function (event) {
                      Carousel.Util.addAutomationRule();
                })
            },
            startAutomationRule: function () {
                var self = this;
                var channelData = self.dao.getChannel();
                if (!Carousel.Util.isCarouselCheck(channelData)) {
                    return;
                }
                self.bindEvent();
                self.initSuggest();
                self.initDialogs();
                self.initRuleList();
                self.fillRuleListGrid();
            },
            initRuleList: function () {
                var self = this;
                var gridConfig = self.getGridConfig(self.config.TABLE_AUTOMATION_RULE);
                self.$grid.jqGrid(gridConfig);

                self.$grid.jqGrid('navGrid', "#pagerAutoRule", {
                    edit: false,
                    add: false,
                    del: false,
                    search: false,
                    refresh: false
                });
                self.$grid.jqGrid('navButtonAdd', "#pagerAutoRule", {
                    caption: "批量删除", buttonicon: "ui-icon ui-icon-trash",
                    onClickButton: function () {
                        var gridElement = $("#ruleList");
                        var dataOriginl = gridElement.jqGrid("getRowData");
                        var interruptFlag = false;
                        $.each(dataOriginl, function (i, rowData) {
                            if (JSON.parse(rowData.hiddenData).status == 1) {
                                interruptFlag = true;
                                return false;
                            }
                        })
                        if (interruptFlag) {
                            SystemDialog.queryAlert("规则执行中不能删除");
                            return;
                        }

                        var selectedtrArr = gridElement.find("tr[aria-selected=true]");
                        var ids = [];
                        $.each(selectedtrArr, function (i, element) {
                            ids.push(parseInt($(element).attr("id")));
                        });

                        if (ids.length == 0) {
                            return;
                        }
                        Carousel.Util.batchDelAutomationRule(ids);
                    }, position: "last", title: "批量删除", cursor: "pointer"
                });
            },
            fillRuleListGrid:function(){
                var self = this;
                var channelData = self.dao.getChannel();
                self.$grid.clearGridData();
                self.$grid.setGridParam({page: 1, url: EpgWeb.urlConfig.automationListUrl, loadtext: '加载中...',
                    datatype: "json",
                    loadui: "block", postData: {channelId: channelData.id}}).trigger("reloadGrid");
            }
        })
    }
);